﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BotonJXC.Entity;
using NHibernate.Criterion;
using BotonJXC.Util;

namespace BotonJXC.DAO
{
    public class OrdersDAO :NHibernateDao<Orders,int>
    {
        public void saveOrders(Orders orders)
        {
            base.Save(orders);
        }


        public IList<Orders> findOrdersBySn(string orderSn)
        {
            List<ICriterion> criterions = new List<ICriterion>();
            if (Utils.IsNotBlank(orderSn)) {
                criterions.Add(Restrictions.Eq("OrderSn", orderSn));
            }
            return base.Find(criterions.ToArray());
        }


        public PageModel<Orders> findOrdersByCondition(DateTime? begintime, DateTime? endtime, Orders orders, int start, int count)
        {
            List<ICriterion> criterions = new List<ICriterion>();
            if (begintime != null) {
                criterions.Add(Restrictions.Ge("CreateTime",begintime));
            }
            if (endtime != null)
            {
                criterions.Add(Restrictions.Le("CreateTime", endtime));
            }
            if (orders != null)
            {
                if (Utils.IsNotBlank(orders.OrderSn))
                {
                    criterions.Add(Restrictions.Like("OrderSn", orders.OrderSn, MatchMode.Anywhere));
                }
                if (Utils.IsNotBlank(orders.OutStatus))
                {
                    criterions.Add(Restrictions.Eq("OutStatus", orders.OutStatus));
                }
                if (Utils.IsNotBlank(orders.AuditStatus))
                {
                    criterions.Add(Restrictions.Eq("AuditStatus", orders.AuditStatus));
                }
                if (Utils.IsNotBlank(orders.InStatus))
                {
                    criterions.Add(Restrictions.Eq("InStatus", orders.InStatus));
                }
                if (orders.Department != null && orders.Department.DepId > 0)
                {
                    criterions.Add(Restrictions.Eq("Department.DepId", orders.Department.DepId));
                }
                if (orders.Supplier != null && orders.Supplier.SupplierId > 0)
                {
                    criterions.Add(Restrictions.Eq("Supplier.SupplierId", orders.Supplier.SupplierId));
                }
                if (orders.BigWarehouse != null && orders.BigWarehouse.WarehouseId > 0)
                {
                    criterions.Add(Restrictions.Eq("Warehouse.WarehouseId", orders.BigWarehouse.WarehouseId));
                }
                if (Utils.IsNotBlank(orders.OrderType))
                {
                    criterions.Add(Restrictions.Eq("OrderType", orders.OrderType));
                }
                if (orders.OrgId > 0)
                {
                    criterions.Add(Restrictions.Eq("OrgId", orders.OrgId));
                }
                if (orders.OrgDepId > 0)
                {
                    criterions.Add(Restrictions.Eq("OrgDepId", orders.OrgDepId));
                }
            }
            Order order = new Order("CreateTime", false);
            return base.FindByPage(criterions.ToArray(), order, start, count);
        }

        public IList<Orders> findOrdersBySn(string orderSn, string orderType)
        {
            List<ICriterion> criterions = new List<ICriterion>();
            if (Utils.IsNotBlank(orderSn))
            {
                criterions.Add(Restrictions.Eq("OrderSn", orderSn));
            }
            if (Utils.IsNotBlank(orderType))
            {
                criterions.Add(Restrictions.Eq("OrderType", orderType));
            }
            return base.Find(criterions.ToArray());
        }
    }
}
